CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

   CLK_FREQ = ((_clkmode - xtal1) >> 6) * _xinfreq

  'EEPROM constants

  EE_DEVICE_ADDR                = $A0
  EE_BASE_ADDR                  = $8000
  

{{
EEPROM Upper memory variable map

EE_BASE_ADDR = 0x8000
==============================================================
addr    size    description
==============================================================
0x8000  long    value_to_write

}}


obj

  i2c   :       "Basic_I2C_Driver"                      'requires 0 cogs
  pst   :       "Parallax Serial Terminal"              'requires 1 cog

VAR

'uninitialized variables

  
  long  value_to_write
  long  value_read_back
  

pub go
{
  Initial program execution begins here!!!
}

  pst.start(115_200)            'start the serial terminal
  i2c.Initialize(i2c#BootPin)   'setup the I2C object

  pst.clear                     'clear the terminal

  value_to_write := 1234_8765
  
  pst.str(string("Writing to EEPROM, this value: "))
  pst.dec(value_to_write)
  pst.char(13)                  'new line

  'write our new calibration data to EEPROM
  i2c.WriteLong(i2c#BOOTPIN, EE_DEVICE_ADDR, constant(EE_BASE_ADDR + $0), value_to_write)

  
  pst.str(string("Done Writing.", 13))
  
  
  PauseMS(1000)                 'wait 1 second

  pst.str(string("Reading back from EEPROM", 13))
  
  value_read_back := i2c.ReadLong(i2c#BOOTPIN, EE_DEVICE_ADDR, constant(EE_BASE_ADDR + $0))

  pst.str(string("Done Reading back", 13))

  pst.str(string("Value read back from EEPROM: "))
  pst.dec(value_read_back)
  pst.char(13)                  'new line

  PauseMS(1000)                 'superfluous 1 second wait
  
  pst.str(string("All done!!!", 13))



pri PauseMS(Duration)
''  Pause execution for 'duration' milliseconds.
''  This routine is based on the set clock frequency.
''  
''  params:  Duration = number of milliseconds to delay                                                                                               
''  return:  none

  waitcnt((((CLK_FREQ / 1_000) * Duration - 3932) #> 381) + cnt)